// 统一管理用户相关的接口
import request from "@/utils/request";
import type { uploadResponseData } from "./type";
// 统一管理接口
enum API {
    UPLOAD_URL = "/need/file/uploadFile",
    DOWNLOAD_URL = "/need/file/download",
    REMOVEUSELESS_URL = "/need/file/deleteUseless",
    DELETEFILE_URL = "/need/file/deleteFile",
}

// 暴露请求函数
// 上传文件
export const reqUpload = (formData: FormData) => {
    return request.post<any, uploadResponseData>(API.UPLOAD_URL, formData);
};
// 下载文件
export const reqDownload = (save_path: string, save_name: string) => {
    return request
        .get<any, ResponseData<string>>(API.DOWNLOAD_URL + "?save_path=" + save_path, {
            responseType: "blob",
        })
        .then((result) => {
            if (result instanceof Blob) {
                const a = document.createElement("a");
                const url = window.URL.createObjectURL(result);
                a.href = url;
                a.download = save_name;
                a.click();
                window.URL.revokeObjectURL(url);
                return {
                    code: 200,
                    data: "下载成功",
                    message: "请求成功",
                };
            } else {
                return result;
            }
        });
};
// 富文本内容多余图片清除
export const reqRemoveUseless = (data: { nameList: Array<string> }) => {
    return request.post<any, ResponseData>(API.REMOVEUSELESS_URL, data);
};
// 删除文件
export const reqDeleteFile = (data: { fileList: Array<{ id: number; name: string }> }) => {
    return request.post<any, ResponseData>(API.DELETEFILE_URL, data);
};
